# -*- coding: utf-8 -*-

import sys
import unicodedata
import os	
import time


def console_list(lista, cols):

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )

		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '|' )

		value = list_item['name']
		if 'title' in list_item:
			value = list_item['title']

		chars = list_item['chars']

		align = 'left'
		if 'align' in list_item:
			align = list_item['align']

		if align == 'left':
			value = value.ljust( chars )
		if align == 'right':
			value = value.rjust( chars )

		sys.stdout.write( ' ' + value + ' ' )

	sys.stdout.write( '|' )
	sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )
		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )

	#import string
	#valid_chars = "-_.() %s%s" % (string.ascii_letters, string.digits)

	for s in lista:
		for list_item in cols:
			sys.stdout.write( '|' )
			value = str(s.__dict__[ list_item['name'] ])
			chars = list_item['chars']
			if len(value) > chars:
				value = s.__dict__[ list_item['name'] ][:chars-4] + ' ...'

			try:
				#value = ''.join(c for c in value if c in valid_chars)
				value = ''.join((c for c in unicodedata.normalize('NFD', unicode(value, 'utf-8') ) if unicodedata.category(c) != 'Mn'))
				#value = ''.join((c for c in unicodedata.normalize('NFD', value.encode( "utf8" ) ) if unicodedata.category(c) != 'Mn'))
			except:
				value = value = ''.join((c for c in unicodedata.normalize('NFD', value ) if unicodedata.category(c) != 'Mn'))

			align = 'left'
			if 'align' in list_item:
				align = list_item['align']
			if align == 'left':
				value = value.ljust( chars )

			if align == 'right':
				value = value.rjust( chars )

			sys.stdout.write( ' ' + value +  ' ')

		sys.stdout.write( '|' )
		sys.stdout.write( '\n' )

	for list_item in cols:
		sys.stdout.write( '+' )
		for i in range(list_item['chars']):
			sys.stdout.write( '-' )
		sys.stdout.write( '--' )

	sys.stdout.write( '+' )
	sys.stdout.write( '\n' )

def console_details(object, filas, width_tit, width_data):
	
	sys.stdout.write( '+' )
	for i in range(width_tit):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	for i in range(width_data):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	
	for list_item in filas:		

		sys.stdout.write( '\n' )
		sys.stdout.write( '|' )

		value = list_item['name']
		if 'title' in list_item:
			value = list_item['title']

		chars = width_tit - 2

		align = 'right'
		if 'align' in list_item:
			align = list_item['align']

		if align == 'left':
			value = value.ljust( chars )
		if align == 'right':
			value = value.rjust( chars )

		sys.stdout.write( ' ' + value + ' ' )
		
		sys.stdout.write( '|' )
		try:
			value = object.__dict__[ list_item['name'] ]
		except:
			value = ""
		value = value.ljust( width_data - 2 )
		sys.stdout.write( ' ' + value.encode('utf8') + ' ' )
		
		sys.stdout.write( '|' )
	
	sys.stdout.write( '\n' )
	sys.stdout.write( '+' )
	for i in range(width_tit):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )
	for i in range(width_data):
		sys.stdout.write( '-' )  
	sys.stdout.write( '+' )	
	sys.stdout.write( '\n' )
	pass

def console_show_captcha(url):	
	FILENAME = "recaptcha.jpg" 
	os.system("wget '" + url + "' -O " + FILENAME + " >/dev/null 2>&1")
	
	import subprocess
	import signal
	
	pipe = subprocess.Popen(['display', FILENAME])
	#print os.system("display "+ FILENAME + " &")

	print "Input:\n"
	captcha = raw_input('--> ')
	
	os.kill(pipe.pid, signal.SIGTERM)	
	
	return captcha

def console_wait(delay):
	print "WAIT %s \n" % delay		
	for i in range(int(delay)):
		print_same_line("Waiting: %d  " % int (int(delay)-i) )			
		time.sleep(1)			
	print_same_line( "Ready: ")


#-----------------------
# Base Functions
#-----------------------

def print_same_line(line):
	sys.stdout.flush()
	sys.stdout.write('\r')
	sys.stdout.flush()
	sys.stdout.write( line )

def dump(obj):
  '''return a printable representation of an object for debugging'''
  newobj=obj
  if '__dict__' in dir(obj):
    newobj=obj.__dict__
    if ' object at ' in str(obj) and not newobj.has_key('__type__'):
      newobj['__type__']=str(obj)
    for attr in newobj:
      newobj[attr]=dump(newobj[attr])
  return newobj
